package com.example.easeimpro.controller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.easeimpro.R;

import com.example.easeimpro.controller.activity.ChatSendMsgActivity;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.interfaces.OnItemClickListener;

import com.hyphenate.easeui.modules.conversation.EaseConversationListLayout;
import com.hyphenate.easeui.modules.conversation.model.EaseConversationInfo;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;
import java.util.Map;

public class ChatFragment extends Fragment {


    private EaseConversationListLayout layout;
    private List<EMMessage> mesList;
    private EMMessageListener msgListener = new EMMessageListener() {
        @Override
        public void onMessageReceived(List<EMMessage> messages) {
            layout.loadDefaultData();
           //监听是否有新消息

            mesList = messages;
        }
    };
    private OnItemClickListener onItemListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            //监听是否有新消息


            Log.e(TAG, "onItemClick: "+  view);;
                TextView textView = view.findViewById(com.hyphenate.easeui.R.id.name);
             String  id =  textView.getText().toString();
            EMConversation conversation = EMClient.getInstance().chatManager().getConversation(id);
            Intent intent = new Intent(getContext(), ChatSendMsgActivity.class);
            Log.e(TAG, "onItemClick: "+conversation.getType() );
            if(conversation.getType() == EMConversation.EMConversationType.Chat){
                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_SINGLE);
            }else if(conversation.getType() == EMConversation.EMConversationType.GroupChat){
                //群聊
                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_GROUP);
            }
            intent.putExtra("conversationId",id);
            startActivity(intent);
        }
    };


    private void toGroupMessage(String name){

        List<EMGroup> group;
        try {
            group = EMClient.getInstance().groupManager().getJoinedGroupsFromServer();
            for(EMGroup em : group){

            }
        } catch (HyphenateException e) {
            throw new RuntimeException(e);
        }

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chat_main_fragment,container,false);
        layout = view.findViewById(R.id.easeConverLayout);
     layout.init();

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        layout.loadDefaultData();
       // conversations = EMClient.getInstance().chatManager().getAllConversations();

        EMClient.getInstance().chatManager().addMessageListener(msgListener);

        layout.setOnItemClickListener(onItemListener);

    }

    private static final String TAG = "ChatFragment";

    @Override
    public void onDestroy() {
        super.onDestroy();
        EMClient.getInstance().chatManager().removeMessageListener(msgListener);
    }
}
